﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CmccTool
{
    internal class AppConfig
    {

        #region 路径
        /// <summary>
        /// 数据库路径
        /// </summary>
        public readonly static string dbPath = Path.Combine(
           GetSafeAppDataPath(),
           "db",
           "CmccTool.db"
        );


        /// <summary>
        ///     查询输出路径
        /// </summary>
        /// <returns>输出路径</returns>
        public static string GetSafeAppDataPath()
        {


            string appDataPath = Path.GetDirectoryName(Environment.ProcessPath) // 程序启动路径
                ?? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) // appdata 路径
                ?? Environment.GetEnvironmentVariable("APPDATA")// APPDATA 环境变量
                ?? string.Empty;


            // 3. 降级方案1：基于UserProfile拼接
            if (string.IsNullOrEmpty(appDataPath))
            {
                string userProfile = Environment.GetEnvironmentVariable("UserProfile") ?? string.Empty;
                if (!string.IsNullOrEmpty(userProfile))
                {
                    appDataPath = Path.Combine(userProfile, "AppData", "Roaming");
                }
            }

            // 4. 降级方案2：应用安装目录
            if (string.IsNullOrEmpty(appDataPath))
            {
                appDataPath = AppDomain.CurrentDomain.BaseDirectory;
            }

            // 确保路径格式合法
            return Path.GetFullPath(appDataPath);
        }

        #endregion

        #region 数据库-数据导入-地区筛选
        /// <summary>
        /// 筛选数据
        /// </summary>
        public readonly static string InsertCompany = "合川";

        public static string InsertCompanyOmc =>
            _CompanyMapping.TryGetValue(InsertCompany, out var mapping)
                ? mapping ?? string.Empty
                : string.Empty;

        private static readonly Dictionary<string , string >
            _CompanyMapping = new(StringComparer.OrdinalIgnoreCase)
            {
                { "合川", "HC"},
            };
        #endregion

        #region 数据库-数据核对-查询配置
        public class SqlQueryConfig
        {
            public required string TableName { get; set; }      // 表名
            public required string DateField { get; set; }       // 日期字段名
            public required string ConditionField { get; set; }  // 条件字段名
            public required string ConditionValue { get; set; }  // 条件值
            public required string OutputAlias { get; set; }     // 输出字段别名
        }

        public readonly static List<SqlQueryConfig> KpiCheckConfigs =
        [
            new() {
                TableName = "kpixwy",
                DateField = "date",
                ConditionField = "sys",
                ConditionValue = "2G",
                OutputAlias = "Xwy2G"
            },
            new() {
                TableName = "kpixwy",
                DateField = "date",
                ConditionField = "sys",
                ConditionValue = "4G",
                OutputAlias = "Xwy4G"
            },
            new() {
                TableName = "kpixwy",
                DateField = "date",
                ConditionField = "sys",
                ConditionValue = "5G",
                OutputAlias = "Xwy5G"
            },
            new() {
                TableName = "kpiomc",
                DateField = "date",
                ConditionField = "sys",
                ConditionValue = "4G",
                OutputAlias = "Omc4G"
            },
            new() {
                TableName = "kpiomc",
                DateField = "date",
                ConditionField = "sys",
                ConditionValue = "5G",
                OutputAlias = "Omc5G"
            },
            new() {
                TableName = "Kpixwylteld",
                DateField = "date",
                ConditionField = "",
                ConditionValue = "",
                OutputAlias = "XwyLteLd"
            },
            new() {
                TableName = "KpixwyNrld",
                DateField = "date",
                ConditionField = "",
                ConditionValue = "",
                OutputAlias = "XwyNrLd"
            },
            new() {
                TableName = "KpixwyNrdx",
                DateField = "date",
                ConditionField = "",
                ConditionValue = "",
                OutputAlias = "XwyNrdx"
            }

        ];
        #endregion
    }
}
